package com.raja.servingStation.common.util;

import com.alibaba.fastjson.JSON;
import com.raja.servingStation.common.constant.Constants;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;

/**
 * Created by liuyanjun
 * on 2017/4/13.
 */
@Component
public class ResponseUtil {
    private Logger log = LoggerFactory.getLogger(ResponseUtil.class);

    @Autowired
    private JwtUtil jwtUtil;
    /**
     * 请求返回数据处理
     * @param res
     * @return
     */
    public   ResponseEntity<String> general(CommonResponse res){
        log.debug(new ResponseEntity<String>(JSON.toJSONString(res), HttpStatus.OK).toString());
        return new ResponseEntity<String>(JSON.toJSONString(res), HttpStatus.OK);
    }

    /**
     * 成功请求
     * @param data  数据
     * @param data  token
     * @return
     */
    public   ResponseEntity<String> success(Object data) {
        CommonResponse res = new CommonResponse();
        res.setCode(Constants.RESCODE_SUCCESS);
        res.setData(data);
        String token = jwtUtil.createJWT(Constants.JWT_ID, null, Constants.JWT_TTL);
        res.setToken(token);
        return general(res);
    }

    public ResponseEntity<String> success(Object data, Integer userId) {
        CommonResponse res = new CommonResponse();
        res.setCode(Constants.RESCODE_SUCCESS);
        res.setData(data);
        String token = jwtUtil.createJWT(Constants.JWT_ID,  userId.toString(), Constants.JWT_TTL);
        res.setToken(token);
        return general(res);
    }


    public   ResponseEntity<String> success(Integer userId){
        CommonResponse res = new CommonResponse();
        res.setCode(Constants.RESCODE_SUCCESS);
        String token = jwtUtil.createJWT(Constants.JWT_ID, userId.toString(), Constants.JWT_TTL);
        res.setToken(token);
        return general(res);
    }

    /**
     * 未登录状态
     * @return
     */
    public   ResponseEntity<String> noLogin(){
        CommonResponse res = new CommonResponse();
        res.setCode(Constants.RESCODE_NOLOGIN);
        return general(res);
    }

    public   ResponseEntity<String> success(String msg) {
        CommonResponse res = new CommonResponse();
        res.setCode(Constants.RESCODE_SUCCESS);
        res.setMsg(msg);
        String token = jwtUtil.createJWT(Constants.JWT_ID, null, Constants.JWT_TTL);
        res.setToken(token);
        return general(res);
    }
    public   ResponseEntity<String> success() {
        CommonResponse res = new CommonResponse();
        res.setCode(Constants.RESCODE_SUCCESS);
        String token = jwtUtil.createJWT(Constants.JWT_ID, null, Constants.JWT_TTL);
        res.setToken(token);
        return general(res);
    }


    /**
     * 请求抛出异常
     * @param msg
     * @return
     */
    public   ResponseEntity<String> exception(String msg){
        CommonResponse res = new CommonResponse();
        res.setCode(Constants.RESCODE_EXCEPTION);
        res.setMsg(msg);
//        String token = jwtUtil.createJWT(Constants.JWT_ID, null, Constants.JWT_TTL);
//        res.setToken(token);
        return general(res);
    }

    /**
     * 请求抛出异常
     * @param exception
     * @return
     */
    public   ResponseEntity<String> exception(VSException exception){
        CommonResponse res = new CommonResponse();
        res.setCode(exception.getErrorCode());
        res.setMsg(exception.getOutMsg());
        String token = jwtUtil.createJWT(Constants.JWT_ID, null, Constants.JWT_TTL);
        res.setToken(token);
        return general(res);
    }

    /**
     * 请求抛出异常
     * @param errorCode
     * @return
     */
    public   ResponseEntity<String> exception(ErrorCodes errorCode){
        CommonResponse res = new CommonResponse();
        res.setCode(errorCode.getErrorCode());
        res.setMsg(errorCode.getOutMsg());
        String token = jwtUtil.createJWT(Constants.JWT_ID, null, Constants.JWT_TTL);
        res.setToken(token);
        return general(res);
    }

    public   ResponseEntity<String> unKonwException(){
        CommonResponse res = new CommonResponse();
        res.setCode(Constants.RESCODE_EXCEPTION);
        res.setMsg("请稍后再试");
        return general(res);
    }

    /**
     * 自定义
     * @param code
     * @param msg
     * @return
     */
    public   ResponseEntity<String> custom(Integer code, String msg) throws Exception{
        CommonResponse res = new CommonResponse();
        res.setCode(code);
        res.setMsg(msg);
        String token = jwtUtil.createJWT(Constants.JWT_ID, null, Constants.JWT_TTL);
        res.setToken(token);
        return general(res);
    }

    public ResponseEntity exception(ReturnCodes errorCode) {
        CommonResponse res = new CommonResponse();
        res.setCode(errorCode.getErrorCode());
        res.setMsg(errorCode.getOutMsg());
//        String token = jwtUtil.createJWT(Constants.JWT_ID, null, Constants.JWT_TTL);
//        res.setToken(token);
        return general(res);
    }

    /**
     * 不需要token的成功返回
     * @return
     */
    public ResponseEntity noTokenSuccess(ReturnCodes code) {
        CommonResponse res = new CommonResponse();
        res.setCode(Constants.RESCODE_SUCCESS);
        res.setMsg(code.getOutMsg());
        return general(res);
    }
}
